﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Collections.Generic;

using Microsoft.AspNetCore.Mvc;
using ZhonTai.DynamicApi.Attributes;
using ZhonTai.Industry.Core.Consts;
using ZhonTai.DynamicApi;
using ZhonTai.Industry.Services.Feature.Dto.Output;
using ZhonTai.Industry.Services.Feature.Dto.Input;
using ZhonTai.Industry.Domain.Feature;
using ZhonTai.Admin.Core.Dto;
using ZhonTai.Admin.Services;

namespace ZhonTai.Industry.Services.Feature
{
    /// <summary>
    /// 工站功能服务
    /// </summary>
    [DynamicApi(Area = IndustryConsts.AreaName)]
    public class FeatureService : BaseService, IFeatureService, IDynamicApi
    {
        private readonly IFeatureRepository _featureRepository;

        public FeatureService(IFeatureRepository featureRepository)
        {
            _featureRepository = featureRepository;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FeatureGetOutput> GetAsync(long id)
        {
            var output = await _featureRepository.GetAsync<FeatureGetOutput>(id);
            return output;
        }

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IEnumerable<FeatureGetListOutput>> GetListAsync(FeatureGetListInput input)
        {
            var list = await _featureRepository.Select
                .OrderByDescending(a => a.Id)
                .ToListAsync<FeatureGetListOutput>();
            return list;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<FeatureGetPageOutput>> GetPageAsync(PageInput<FeatureGetPageInput> input)
        {
            var filter = input.Filter;
            var list = await _featureRepository.Select
                .WhereDynamicFilter(input.DynamicFilter)
                .WhereIf(filter != null && !string.IsNullOrEmpty(filter.Description), a => a.Description == filter.Description)
                .Count(out var total)
                .OrderByDescending(c => c.Id)
                .Page(input.CurrentPage, input.PageSize)
                .ToListAsync<FeatureGetPageOutput>();

            //关联查询代码

            var data = new PageOutput<FeatureGetPageOutput> { List = list, Total = total };

            return data;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<long> AddAsync(FeatureAddInput input)
        {
            var entity = Mapper.Map<FeatureEntity>(input);
            var id = (await _featureRepository.InsertAsync(entity)).Id;

            return id;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task UpdateAsync(FeatureUpdateInput input)
        {
            var entity = await _featureRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                throw ResultOutput.Exception("工站功能不存在！");
            }

            Mapper.Map(input, entity);
            await _featureRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> DeleteAsync(long id)
        {
            return await _featureRepository.DeleteAsync(id) > 0;
        }

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> SoftDeleteAsync(long id)
        {
            return await _featureRepository.SoftDeleteAsync(id);
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<bool> BatchSoftDeleteAsync(long[] ids)
        {
            return await _featureRepository.SoftDeleteAsync(ids);
        }
    }
}